package com.addplus.townmall.server.api.mapper.town;

import com.addplus.townmall.server.api.model.town.TownOrderFight;
import com.addplus.townmall.server.api.model.town.expand.TownOrderFightExpand;
import com.addplus.townmall.server.api.model.town.param.OrderFightParam;
import com.addplus.townmall.server.api.model.town.param.OrderItemsFightParam;
import com.addplus.townmall.server.api.utils.BaseAddMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * <p>
 * 商城拼团商品订单表 Mapper 接口
 * </p>
 *
 * @author zhangjiehang
 * @since 2018-10-30
 */
public interface TownOrderFightMapper extends BaseAddMapper<TownOrderFight> {

    @Select("SELECT\n" +
            " p.store_id AS shop_store_id,\n" +
            " p.id AS shop_product_id,\n" +
            " p.shop_name,\n" +
            " p.shop_seal_img,\n" +
            " p.shop_fright_id,\n" +
            " p.fright_type,\n" +
            " p.fright,\n" +
            " p.shop_customize_code,\n" +
            " p.store_id,\n" +
            " p.shop_tag,\n" +
            " p.group_time," +
            " p.group_num," +
            " p.is_only," +
            " p.is_more," +
            " p.colonel," +
            " p.online_status," +
            " p.buy_limit," +
            " p.one_buy_limit," +
            " sku.sale_price,\n" +
            " sku.cost_price,\n" +
            " sku.discount_price,\n" +
            " sku.stock,\n" +
            " sku.id AS shop_specification_sku_id,\n" +
            " sku.sku_id AS shop_specification_sku_sku_id,\n" +
            " sku.sku_describe AS shop_specification_sku_json,\n" +
            " IFNULL( c.enable_noreason_return, 0 ) AS enable_noreason_return \n" +
            "FROM\n" +
            " shop_product_fight p\n" +
            " LEFT JOIN shop_specification_sku sku ON sku.shop_product_id = p.id \n" +
            " AND sku.is_deleted = 0\n" +
            " LEFT JOIN shop_participate_active a ON sku.shop_product_id = a.shop_product_id \n" +
            " AND a.shop_sku_id = sku.id \n" +
            " AND a.`status` = 1 \n" +
            " AND a.is_deleted = 0\n" +
            " LEFT JOIN platform_category c ON p.shop_category_id = c.id \n" +
            " AND c.is_deleted = 0 \n" +
            "WHERE\n" +
            " sku.id =#{skuId} \n" +
            " AND p.audit_status = 0 AND p.is_deleted = 0 AND p.sale_status = 0\n")
    OrderItemsFightParam getConfirmOrderItemsMessage(@Param("skuId") Integer skuId);


    @Select("SELECT\n" +
            "\ts.id AS store_id,\n" +
            "\ts.store_name,\n" +
            "\ts.icon_path,\n" +
            "\ts.sender_phone AS seller_phone,\n" +
            "\ts.store_type,\n" +
            "\ts.sender_address AS seller_address,\n" +
            "\ts.freight,\n" +
            "\ts.free_freight\n" +
            "FROM\n" +
            "\tstore s\n" +
            "WHERE\n" +
            "\ts.id = #{storeId}  AND s.is_delete = 0 AND s.store_status = 1")
    TownOrderFight getConfirmOrderMessage(@Param("storeId") Integer storeId);

    @Select("SELECT\n" +
            " f.id,\n" +
            " f.order_number,\n" +
            " f.order_status,\n" +
            " f.order_type,\n" +
            " f.close_reason,\n" +
            " f.member_id,\n" +
            " f.member_remark,\n" +
            " f.freight,\n" +
            " f.store_id,\n" +
            " f.personal_store_id,\n" +
            " f.icon_path,\n" +
            " f.store_name,\n" +
            " f.colonel,\n" +
            " f.shop_total_num,\n" +
            " f.shop_total_amount,\n" +
            " f.discount_amount,\n" +
            " f.total_amount,\n" +
            " f.coupon_id,\n" +
            " f.full_discount,\n" +
            " f.timeout_date,\n" +
            " f.gmt_create,\n" +
            " f.gmt_modified,\n" +
            " i.id AS order_item_id,\n" +
            " i.order_id,\n" +
            " i.store_id,\n" +
            " i.shop_product_id,\n" +
            " i.shop_name,\n" +
            " i.shop_seal_img,\n" +
            " i.sale_price,\n" +
            " i.shop_tag,\n" +
            " i.purchase_quantity,\n" +
            " i.shop_specification_sku_id,\n" +
            " i.shop_specification_sku_json,\n" +
            " i.enable_noreason_return ,\n" +
            " o.invoice_type\n" +
            "FROM\n" +
            " town_order_fight f\n" +
            " INNER JOIN order_items_fight i ON i.order_id = f.id \n" +
            " AND i.is_delete = 0 \n" +
            " LEFT JOIN order_invoice  o on f.id = o.order_id AND o.is_delete = 0 AND o.type = 2\n" +
            "WHERE\n" +
            " f.id = #{orderFightId} \n" +
            " AND f.is_delete = 0 \n" +
            " AND f.member_id = #{memberId}")
    @Results({@Result(property = "orderItemsFightParam.id", column = "order_item_id"),
            @Result(property = "orderItemsFightParam.orderId", column = "order_id"),
            @Result(property = "orderItemsFightParam.storeId", column = "store_id"),
            @Result(property = "orderItemsFightParam.shopProductId", column = "shop_product_id"),
            @Result(property = "orderItemsFightParam.shopName", column = "shop_name"),
            @Result(property = "orderItemsFightParam.shopSealImg", column = "shop_seal_img"),
            @Result(property = "orderItemsFightParam.salePrice", column = "sale_price"),
            @Result(property = "orderItemsFightParam.shopTag", column = "shop_tag"),
            @Result(property = "orderItemsFightParam.purchaseQuantity", column = "purchase_quantity"),
            @Result(property = "orderItemsFightParam.shopSpecificationSkuId", column = "shop_specification_sku_id"),
            @Result(property = "orderItemsFightParam.shopSpecificationSkuJson", column = "shop_specification_sku_json"),
            @Result(property = "orderItemsFightParam.enableNoreasonReturn", column = "enable_noreason_return"),
            @Result(property = "orderInvoice.invoiceType", column = "invoice_type")
    })
    OrderFightParam getConfirmCreateOrder(@Param("orderFightId") Integer orderFightId, @Param("memberId") Integer memberId);


    @Select("SELECT\n" +
            " t.id,\n" +
            " t.order_status,\n" +
            " t.member_id,\n" +
            " t.pay_status,\n" +
            " t.is_group,\n" +
            " t.parent_id,\n" +
            " t.is_success,\n" +
            " IFNULL(count(f.id),0) as fight_count\n" +
            "FROM\n" +
            " town_order_fight t LEFT JOIN town_order_fight f ON f.parent_id = t.id AND f.is_delete = 0 AND t.member_id != f.member_id and f.order_status = 2 \n" +
            "WHERE\n" +
            " t.id = #{orderFightId} \n" +
            " AND t.is_delete = 0 AND t.parent_id = 0 AND t.is_group = 1\n" +
            "GROUP BY\n" +
            " f.id")
    OrderFightParam getShopFightGroupCount(@Param("orderFightId") Integer orderFightId);


    @Select("SELECT\n" +
            " id \n" +
            "FROM\n" +
            "\ttown_order_fight \n" +
            "WHERE\n" +
            " parent_id = #{orderFightId}\n" +
            " AND is_group = 1 \n" +
            " AND is_delete = 0 \n" +
            " AND order_status = 2 \n" +
            " AND pay_status = 4")
    List<Integer> getOrderPartOrderId(@Param("orderFightId") Integer orderFightId);


    @Select("SELECT\n" +
            " f.id,\n" +
            " m.id AS member_id,\n" +
            " m.nick_name,\n" +
            " m.head_portrait \n" +
            "FROM\n" +
            " town_order_fight f\n" +
            " LEFT JOIN member m ON m.id = f.member_id \n" +
            " AND m.is_deleted = 0 \n" +
            "WHERE\n" +
            " (f.id = #{orderId} \n" +
            " OR f.parent_id = #{orderId} )\n" +
            " and f.order_status != 1\n" +
            "ORDER BY\n" +
            " f.id")
    List<TownOrderFightExpand> getOrderPartJoinByOrderId(@Param("orderId") Integer orderId);

    @Select("<script>\n" +
            "select  * " +
            "FROM town_order_fight " +
            " where member_id in (select id from member where invite_member_id=#{memberId}) " +
            " and is_delete=0" +
            "<if test=\"statusType != null and statusType !=-1 and statusType !=''\">\n" +
            " and order_status = #{statusType}" +
            "</if>\n" +
            "</script>")
    List<TownOrderFightExpand> getFriendOrder(Page page, @Param("memberId") Integer memberId, @Param("statusType") Integer statusType);

    @Select("select count(*) from town_order_fight a where a.member_id=#{memberId} and is_delete=0")
    Integer getOrderCount(@Param("memberId") Integer memberId);


}
